document.addEventListener('DOMContentLoaded', function() {
  const toggle = document.getElementById('rtlToggle');
  const fontSelect = document.getElementById('fontSelect');
  const justifyToggle = document.getElementById('justifyToggle');
  const fontSizeSelect = document.getElementById('fontSizeSelect');
  
  // RTL ۋە خەت نۇسخىسى ھالىتىنى ئوقۇش
  chrome.storage.local.get(['rtlEnabled', 'selectedFont', 'justifyEnabled', 'fontSize'], function(result) {
    toggle.checked = result.rtlEnabled || false;
    fontSelect.value = result.selectedFont || '';
    justifyToggle.checked = result.justifyEnabled || false;
    fontSizeSelect.value = result.fontSize || '';
  });

  // RTL كۇنۇپكىسىنىڭ ئۆزگىرىشىنى كۆزىتىش
  toggle.addEventListener('change', function() {
    chrome.storage.local.set({rtlEnabled: toggle.checked});
    
    // 向当前标签页发送消息
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {
        action: 'toggleRTL',
        enabled: toggle.checked
      });
    });
  });

  // تېكىست توغرىلاش كۇنۇپكىسىنىڭ ئۆزگىرىشىنى كۆزىتىش
  justifyToggle.addEventListener('change', function() {
    chrome.storage.local.set({justifyEnabled: justifyToggle.checked});
    
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {
        action: 'toggleJustify',
        enabled: justifyToggle.checked
      });
    });
  });

  // خەت نۇسخىسى تاللاشنىڭ ئۆزگىرىشىنى كۆزىتىش
  fontSelect.addEventListener('change', function() {
    chrome.storage.local.set({selectedFont: fontSelect.value});
    
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {
        action: 'changeFont',
        font: fontSelect.value
      });
    });
  });

  // خەت چوڭلۇقى تاللاشنىڭ ئۆزگىرىشىنى كۆزىتىش
  fontSizeSelect.addEventListener('change', function() {
    chrome.storage.local.set({fontSize: fontSizeSelect.value});
    
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {
        action: 'changeFontSize',
        size: fontSizeSelect.value
      });
    });
  });
}); 